package com.luo.bbs.ums.webapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luo.bbs.ums.service.IUserForAdminService;
import com.luo.bbs.ums.webapi.mapper.BuildingMapper;
import com.luo.bbs.ums.webapi.mapper.UserMapper;
import com.luo.common.exception.BBSServiceException;
import com.luo.common.restful.ResponseCode;
import com.luo.pojo.Building;
import com.luo.pojo.ums.entity.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class UserForAdminServiceImpl implements IUserForAdminService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BuildingMapper buildingMapper;

    @Override
    public List<User> userList() {
        List<User> users = userMapper.selectList(null);
        return users;
    }

    @Override
    public List<User> userListByBuild(String buildingName) {
        QueryWrapper<Building> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", buildingName);
        Building building = buildingMapper.selectOne(queryWrapper);
        if (building == null) {
            throw new BBSServiceException(ResponseCode.BAD_REQUEST, "不存在！");

        }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("building_name", buildingName);
        List<User> users = userMapper.selectList(userQueryWrapper);
        return users;
    }

    @Override
    public List<Building> getBuilding() {
        List<Building> buildings = buildingMapper.selectList(null);
        return buildings;
    }

    @Override
    public int setUserAble(Long id) {
        if (id == null) {
            throw new BBSServiceException(ResponseCode.BAD_REQUEST, "参数不难为空");
        }
        User user = userMapper.selectById(id);
        if (user == null) {
            throw new BBSServiceException(ResponseCode.NOT_FOUND, "用户不存在");
        }
        if (user.getEnabled() == 1) {
            user.setEnabled(0);//禁用
            userMapper.updateById(user);
            return 0;
        }
        user.setEnabled(1);
        userMapper.updateById(user);
        return 1;

    }


}
